package com.jishuo.pzk.util;


import com.jishuo.pzk.bean.base.VouSealVO;
import com.jishuo.pzk.bean.base.VouTempPageVO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;

@Slf4j
@Component
public class GetBlob {
    // 获取数据库连接
    @SneakyThrows
    public Connection getConnection() {
        String dbUrl = "jdbc:mysql://localhost:3306/pzk?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
        String dbUsername = "root";
        String dbPassword = "root";
        return DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
    }


    // 关闭数据库连接的方法
    private static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // 从Blob对象中获取二进制数据的方法
    private static byte[] getBinaryDataFromBlob(Blob blob) throws SQLException, IOException {
        if (blob == null) {
            return null;
        }

        try (InputStream inputStream = blob.getBinaryStream();
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            return outputStream.toByteArray();
        }
    }


    /**
     * 查询模板图片
     * @param id 模板id
     * @return 返回模板二进制对象
     * @throws SQLException 抛出异常
     */
    public VouTempPageVO findVouTemPage(String id) throws SQLException {
        Connection connection = getConnection();
        VouTempPageVO vouTempPageVO = new VouTempPageVO();

        try {
            PreparedStatement statement = connection.prepareStatement("select * from t_fsvoutemppage where id = ?");
            statement.setString(1 ,id);
            ResultSet resultSet = statement.executeQuery();

            if (resultSet.next()) {
                // 获取Blob对象
                Blob blob = resultSet.getBlob("VO_TEMP");

                byte[] data = getBinaryDataFromBlob(blob);
                vouTempPageVO.setTemp(data);
            }
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        } finally {
            closeConnection(connection);
        }
        return vouTempPageVO;
    }

    /**
     * 查询印章图片
     * @param id 印章id
     * @return 返回印章二进制对象
     */
    public VouSealVO findVouSealVO(String id) {
        Connection connection = getConnection();
        VouSealVO vouSealVO = new VouSealVO();

        try {
            PreparedStatement statement = connection.prepareStatement("select * from T_FSVOUSEAL where id = ?");
            statement.setString(1 ,id);
            ResultSet resultSet = statement.executeQuery();

            if (resultSet.next()) {
                // 获取Blob对象
                Blob blob = resultSet.getBlob("SEAL");

                byte[] data = getBinaryDataFromBlob(blob);
                vouSealVO.setSealByte(data);
            }
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        } finally {
            closeConnection(connection);
        }
        return vouSealVO;
    }
}

